﻿using Dapr.Client;
using VaccinationHost.Extend;
using VaccinationHost.Model;
using VaccinationHost.Model.Dto;

namespace VaccinationHost.Services
{
    /// <summary>
    /// 预约接种记录服务
    /// </summary>
    public class VaccinationService: IVaccinationService
    {
        private readonly HttpClient _httpClient;

        public VaccinationService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        /// <summary>
        /// 根据用户获取预约接种记录
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<PaginatedResult<VaccinationDto>> GetVaccinations(VaccinationPageInput pageInput)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, "api/v1/vaccination/info/list")
            {
                Content = JsonContent.Create(pageInput)
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult == null || regresult is OpResult)
            {
                return new PaginatedResult<VaccinationDto>() { Total = 0 };
            }
            else
            {
                return regresult.ToString().ToObject<PaginatedResult<VaccinationDto>>();
            }
        }

        /// <summary>
        /// 预约操作
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="vaccineId"></param>
        /// <param name="vaccineName"></param>
        /// <returns></returns>
        public async Task<OpResult> Appointment(Guid? userId, Guid? vaccineId, string vaccineName, string userName)
        {
            // TODO dapr服务地址待定

            OpResult result = new OpResult();
            if (vaccineId==null)
            {
                result.Code = 400;
                result.Message = "请选择疫苗。";
                return result;
            }
            if (string.IsNullOrWhiteSpace(vaccineName))
            {
                result.Code = 400;
                result.Message = "请选择疫苗。";
                return result;
            }
            if (userId == null)
            {
                result.Code = 401;
                result.Message = "请先登录。";
                return result;
            }
            if (string.IsNullOrWhiteSpace(userName))
            {
                result.Code = 401;
                result.Message = "请先登录。";
                return result;
            }

            var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/vaccination/info")
            {
                Content = JsonContent.Create(new { VaccineId=vaccineId, VaccineName=vaccineName, UserId=userId, UserName=userName, BookedTime=DateTime.Now })
            };

            var response = await _httpClient.SendAsync(request);

            var regresult = await response.ToResult();
            if (regresult is OpResult)
            {
                return regresult as OpResult;
            }
            else
            {
                result.Code = 200;
                result.Message = "预约成功。";
            }

            return result;
        }
    }
}
